package com.platform.mobile.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.platform.bean.PageInfo;
import com.platform.common.dao.impl.CommonDaoImpl;
import com.platform.mobile.bean.ContactInfo;
import com.platform.mobile.bean.FollowedListBean;
import com.platform.mobile.bean.GetHospitalRecordRequest;
import com.platform.mobile.bean.GetPatientRequest;
import com.platform.mobile.bean.LoginAssistant;
import com.platform.mobile.bean.LoginDoctor;
import com.platform.mobile.bean.SearchPaitentRequest;
import com.platform.mobile.bean.TransferBean;
import com.platform.mobile.dao.PaitentDao;
import com.platform.mobile.entity.VisitCourseDisease;
import com.platform.mobile.entity.VisitCourseRes;
import com.platform.mobile.entity.VisitFollowed;
import com.platform.mobile.entity.VisitGroups;
import com.platform.mobile.entity.VisitHospitalRecord;
import com.platform.mobile.entity.VisitUser;
import com.platform.tool.HelpFunctions;

@Repository
public class PaitentDaoImpl extends CommonDaoImpl 
		implements PaitentDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitUser> getUsersList(PageInfo pageInfo) {
		
		pageInfo.init();
		
		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql 
			= new StringBuilder("from VisitUser where 1=1 and org_id = '-1' ");
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),
				params.toArray(),pageInfo);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getTotalCountOfUsers() {
		
		int totalCount = 0;
		
		List list = new ArrayList<Object[]>();
		
		StringBuilder whereSql = new StringBuilder("select count(*) from visit_user");
		List<Object> params = new ArrayList<Object>();
		
		list = getListBySQL(whereSql.toString(), params.toArray(), null, null);
		if(!HelpFunctions.isEmpty(list)){
			/*BigInteger[] o = (BigInteger[]) list.get(0);
			totalCount = o[0].intValue();*/
			totalCount = ((BigInteger)list.get(0)).intValue();
		}
		
		return totalCount;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitUser> getUsersLikeName(SearchPaitentRequest name) {
		
		name.getPageInfo().init();
		
		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql = new StringBuilder("from VisitUser where 1=1 ");
		
		whereSql.append("and real_name like ?");
		params.add("%" + name.getContent() + "%");
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),params.toArray(),name.getPageInfo());
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitUser> getUsersLikePhone(SearchPaitentRequest phone) {
		
		phone.getPageInfo().init();
		
		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql = new StringBuilder("from VisitUser where 1=1 ");
		
		whereSql.append("and phone like ?");
		params.add("%" + phone.getContent() + "%");
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),params.toArray(),phone.getPageInfo());
		
		return list;
	}

	@Override
	public boolean insertUser(VisitUser user) {
		
		create(user);
		
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getUsersLikeNameCount(SearchPaitentRequest name) {
		
		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql = new StringBuilder("from VisitUser where 1=1 ");
		
		whereSql.append("and real_name like ?");
		params.add("%" + name.getContent() + "%");
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),params.toArray(),null);
		
		if(!HelpFunctions.isEmpty(list)){
			return list.size();
		}
		
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getUsersLikePhoneCount(SearchPaitentRequest phone) {

		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql = new StringBuilder("from VisitUser where 1=1 ");
		
		whereSql.append("and phone like ?");
		params.add("%" + phone.getContent() + "%");
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),params.toArray(),null);
		
		if(!HelpFunctions.isEmpty(list)){
			return list.size();
		}
		
		return 0;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitHospitalRecord> getHospitalRecordById(GetHospitalRecordRequest request) {
		
		request.getPageInfo().init();
		
		List<VisitHospitalRecord> list 
			= new ArrayList<VisitHospitalRecord>();
		List<Object> params = new ArrayList<Object>();

		StringBuilder whereSql = new StringBuilder("from VisitHospitalRecord where 1=1 ");
		
		if(!request.getUserId().isEmpty()){
			whereSql.append("and u_id = ?");
			params.add(Integer.parseInt(request.getUserId()));
		}
		
		list = (List<VisitHospitalRecord>) getListByHQL(whereSql.toString(),
				params.toArray(),request.getPageInfo());
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitHospitalRecord> getHospitalRecord(PageInfo pageInfo) {
		
		pageInfo.init();
		
		List<VisitHospitalRecord> list 
			= new ArrayList<VisitHospitalRecord>();
		List<Object> params = new ArrayList<Object>();
	
		StringBuilder whereSql = new StringBuilder("from VisitHospitalRecord where 1=1 ");
		
		list = (List<VisitHospitalRecord>) getListByHQL(whereSql.toString(),
				params.toArray(),pageInfo);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getPatientNameById(Integer id) {
		
		List<VisitUser> list = new ArrayList<VisitUser>();
		List<Object> params = new ArrayList<Object>();
		
		StringBuilder whereSql = new StringBuilder("from VisitUser where 1=1 ");
		
		whereSql.append("and ID = ?");
		params.add(id);
		
		list = (List<VisitUser>) getListByHQL(whereSql.toString(),params.toArray(),null);
		
		if(!HelpFunctions.isEmpty(list)){
			return list.get(0).getReal_name();
		}
		
		return "";
	}

	@Override
	public boolean insertHospitalRecord(VisitHospitalRecord record) {
		
		create(record);
		
		return true;
	}

	@Override
	public int getTotalCountOfHospitalRecord() {
		int totalCount = 0;
		
		List list = new ArrayList<Object[]>();
		
		StringBuilder whereSql = new StringBuilder("select count(*) from visit_hospital_record");
		List<Object> params = new ArrayList<Object>();
		
		list = getListBySQL(whereSql.toString(), params.toArray(), null, null);
		if(!HelpFunctions.isEmpty(list)){
			totalCount = ((BigInteger)list.get(0)).intValue();
		}
		
		return totalCount;
	}

	@Override
	public String getUserNameById(String userId) {
		
		
		return null;
	}

	@Override
	public boolean insertCourseDisease(VisitCourseDisease course) {
		
		create(course);
		
		return true;
	}

	@Override
	public boolean insertCourseRes(VisitCourseRes res) {
		
		create(res);
		
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitCourseDisease> getCourseList(Integer recordId) {
		
		List<VisitCourseDisease> list = new ArrayList<VisitCourseDisease>();
		StringBuilder whereSql = new StringBuilder("from VisitCourseDisease where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		
		if(recordId != null){
			
			whereSql.append("and n_record_id = ?");
			params.add(recordId);
			
			list = (List<VisitCourseDisease>) getListByHQL(whereSql.toString()
					,params.toArray(),null);
		} 
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitCourseRes> getCourseResList(String courseId) {
		
		List<VisitCourseRes> list = new ArrayList<VisitCourseRes>();
		StringBuilder whereSql = new StringBuilder("from VisitCourseRes where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		
		whereSql.append("and c_course_id = ?");
		params.add(courseId);
		
		list = (List<VisitCourseRes>) getListByHQL(whereSql.toString()
				,params.toArray(),null);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitFollowed> getFollowedListByRecrodId(Integer recordId) {
		
		List<VisitFollowed> list = new ArrayList<VisitFollowed>();
		StringBuilder whereSql = new StringBuilder("from VisitFollowed where 1=1 ");
		List<Object> params = new ArrayList<Object>();
		
		whereSql.append("and HOSPITAL_RECORD_ID = ?");
		params.add(recordId);
		
		list = (List<VisitFollowed>) getListByHQL(whereSql.toString()
				,params.toArray(),null);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FollowedListBean> getFollowedView(Integer aidId, boolean isFollowed
			,String content,String disease, Integer times,PageInfo pageInfo) {

		pageInfo.init();
		
		List<FollowedListBean> list = new ArrayList<FollowedListBean>();
		StringBuilder whereSql 
			= new StringBuilder("SELECT u.REAL_NAME AS name, o.ORG_NAME AS deptName, "
					+ "r.major AS doctor, r.note AS note, r.inhiscode AS inhiscode, "
					+ "r.ADMISSION_DATE AS inDate, r.DISCHARGE_DATE AS outDate,"
					+ "(SELECT COUNT(*) FROM visit_followed f WHERE f.HOSPITAL_RECORD_ID = r.ID) AS followedTimes "
					+ "FROM visit_hospital_record r "
					+ "LEFT JOIN visit_org o ON r.dept_id = o.ID "
					+ "LEFT JOIN visit_user u ON r.u_id = u.ID "
					+ "WHERE r.aid_id = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(aidId);
		
		if(times != null && times >= 0){
			whereSql.append("AND (SELECT COUNT(*) "
					+ "FROM visit_followed "
					+ "WHERE HOSPITAL_RECORD_ID = r.ID) = ? ");
			params.add(times);
		} else {
			if(isFollowed){
				whereSql.append("AND (SELECT COUNT(*) "
						+ "FROM visit_followed "
						+ "WHERE HOSPITAL_RECORD_ID = r.ID) > 0 ");
			} else {
				whereSql.append("AND (SELECT COUNT(*) "
						+ "FROM visit_followed "
						+ "WHERE HOSPITAL_RECORD_ID = r.ID) = 0 ");
			}
		}
		
		if(content != null && !content.isEmpty()){
			whereSql.append("AND u.REAL_NAME like ? ");
			params.add("%" + content + "%");
		}
		
/*		if(disease != null && !disease.isEmpty()){
			
		}
		*/
		
		list = (List<FollowedListBean>) getListBySQL(whereSql.toString(), params.toArray()
				,FollowedListBean.class,pageInfo);
		return list;
	}

	@Override
	public int getTotalCountOfFollowedView(Boolean isFollowed
			,String content,String disease, Integer times) {
		
		int totalCount = 0;
		
		List list = new ArrayList<Object[]>();
		
		StringBuilder whereSql = new StringBuilder("SELECT COUNT(*) "
				+ "FROM visit_hospital_record r "
				+ "LEFT JOIN visit_org o ON r.dept_id = o.ID "
				+ "LEFT JOIN visit_user u ON r.u_id = u.ID ");
		List<Object> params = new ArrayList<Object>();
		
		if(times != null && times != 0){
			whereSql.append("AND (SELECT COUNT(*) "
					+ "FROM visit_followed "
					+ "WHERE HOSPITAL_RECORD_ID = r.ID) = ? ");
			params.add(times);
		} else {
			if(isFollowed){
				whereSql.append("AND (SELECT COUNT(*) "
						+ "FROM visit_followed "
						+ "WHERE HOSPITAL_RECORD_ID = r.ID) > 0 ");
			} else {
				whereSql.append("AND (SELECT COUNT(*) "
						+ "FROM visit_followed "
						+ "WHERE HOSPITAL_RECORD_ID = r.ID) = 0 ");
			}
		}
		
		if(content != null && !content.isEmpty()){
			whereSql.append("AND r.note like ? ");
			params.add("%" + content + "%");
		}
		
/*		if(disease != null && !disease.isEmpty()){
			
		}
		*/
		
		list = getListBySQL(whereSql.toString(), params.toArray(), null, null);
		if(!HelpFunctions.isEmpty(list)){
			totalCount = ((BigInteger)list.get(0)).intValue();
		}
		
		return totalCount;
	}

	@Override
	public boolean insertFollowed(VisitFollowed followed) {
		
		create(followed);
		
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TransferBean> getTransferView(Integer deptId, boolean isTransfer
			,String content,PageInfo pageInfo) {
	
		pageInfo.init();
		
		List<TransferBean> transferList = new ArrayList<TransferBean>();
		StringBuilder whereSql 
			= new StringBuilder("SELECT t.REAL_NAME AS realName,"
					+ "t.SEX AS sex, t.PHONE AS phone, o.ORG_NAME AS orgName, "
					+ "u.REAL_NAME AS doctor, t.MEDICAL_INTRODUCTION AS note,"
					+ "t.CREATE_DATE AS createDate "
					+ "FROM visit_transfer_treatment t "
					+ "LEFT JOIN visit_org o ON o.ID = t.OUT_ORG_ID "
					+ "LEFT JOIN visit_user u ON u.ID = t.SEND_USER_ID "
					+ "WHERE 1=1 ");
		
		List<Object> params = new ArrayList<Object>();
		
		if(content != null && !content.isEmpty()){
			
			//whereSql.append("AND ");
			
		} else {
			
			if(isTransfer){
				whereSql.append("AND t.STATUS = 2 ");
			} else {
				whereSql.append("AND t.STATUS = 1 ");
			}
			
		}
		
		whereSql.append("AND t.RECEIVE_DEPT_ID = ? ");
		params.add(deptId);
		
		transferList = (List<TransferBean>) getListBySQL(whereSql.toString()
				,params.toArray(),
				TransferBean.class,pageInfo);
		
		return transferList;
	}

	@Override
	public int getTotalCountOfTransferView(Integer deptId, boolean isTransfer, String content) {
		
		List transferList = new ArrayList<>();
		StringBuilder whereSql 
			= new StringBuilder("SELECT COUNT(*) "
					+ "FROM visit_transfer_treatment t "
					+ "LEFT JOIN visit_org o ON o.ID = t.OUT_ORG_ID "
					+ "LEFT JOIN visit_user u ON u.ID = t.SEND_USER_ID "
					+ "WHERE 1=1 ");
		
		List<Object> params = new ArrayList<Object>();
		
		if(content != null && !content.isEmpty()){
			
			//whereSql.append("AND ");
			
		} else {
			
			if(isTransfer){
				whereSql.append("AND t.STATUS = 2 ");
			} else {
				whereSql.append("AND t.STATUS = 1 ");
			}
			
		}
		
		whereSql.append("AND t.RECEIVE_DEPT_ID = ? ");
		params.add(deptId);
		
		transferList = getListBySQL(whereSql.toString(),params.toArray(),null,null);
		
		if(!HelpFunctions.isEmpty(transferList)){
			return transferList.size();
		}
		
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginDoctor> getLoginDoctor(Integer userId) {
		List<LoginDoctor> doctorList = new ArrayList<LoginDoctor>();
		List<Object[]> list = new ArrayList<Object[]>();
		StringBuilder whereSql = 
				new StringBuilder("SELECT u.LOGIN_NAME AS doctor_phone,"
						+ "u.ID AS ID "
						+ "FROM visit_contact c "
						+ "LEFT JOIN visit_user u ON c.d_id = u.ID "
						+ "WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		
		if(userId != null){
			whereSql.append("AND c.u_id = ? ");
			params.add(userId);
		}
		
		list =  (List<Object[]>)getListBySQL(whereSql.toString(),params.toArray(),
				null, null);
		
		if(!HelpFunctions.isEmpty(list)){
			for(Object[] obj : list){
				LoginDoctor doctor = new LoginDoctor();
				
				if(obj[0] != null){
					doctor.setDoctor_phone(obj[0].toString());
				}
				
				if(obj[1] != null){
					doctor.setID((Integer)obj[1]);
				}
				doctorList.add(doctor);
			}
		}
		
		return doctorList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LoginAssistant> getLoginAssistantList(Integer userId) {
		
		List<LoginAssistant> list = new ArrayList<LoginAssistant>();
		StringBuilder whereSql 
			= new StringBuilder("SELECT u1.LOGIN_NAME AS assistant_phone "
					+ "FROM visit_user u1 "
					+ "LEFT JOIN visit_user u2 ON u2.DEPT_ID = u1.DEPT_ID "
					+ "WHERE 1=1 AND u1.USER_TYPE = '3' ");
		
		List<Object> params = new ArrayList<Object>();
		
		if(userId != null){
			whereSql.append("AND u2.ID = ? ");
			params.add(userId);
		}
		
		list = (List<LoginAssistant>) getListBySQL(whereSql.toString(), params.toArray(),
				LoginAssistant.class, null);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VisitGroups> getGroupsByType(Integer userId) {
		
		List<VisitGroups> list = new ArrayList<VisitGroups>();
		StringBuilder whereSql = new StringBuilder("SELECT * FROM visit_groups "
				+ "WHERE u_id IN (?,0) AND groupType = 0 ORDER BY ID");
		List<Object> params = new ArrayList<Object>();
		
		params.add(userId);
		
		list = (List<VisitGroups>) getListBySQL(whereSql.toString(), params.toArray(),
				VisitGroups.class,null);
		
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ContactInfo> getUsersByGroupIdAndUId(Integer groupId, Integer userId) {

		List<ContactInfo> list = new ArrayList<ContactInfo>();
		StringBuilder whereSql = new StringBuilder("SELECT u.LOGIN_NAME AS phone, u.REAL_NAME AS name,"
				+ " u.thumb AS picture, u.SEX AS sex, "
				+ "u.address AS address, u.BIRTHDAY AS birthday "
				+ "FROM visit_user u "
				+ "WHERE ID IN "
				+ "(SELECT u_id FROM visit_contact WHERE g_id = ? AND d_id = ?) "
				+ "AND STATUS != 2");
		
		List<Object> params = new ArrayList<Object>();
		params.add(groupId);
		params.add(userId);
		
		list = (List<ContactInfo>) getListBySQL(whereSql.toString(), params.toArray(),
				ContactInfo.class,null);
		return list;
	}


	@SuppressWarnings("unchecked")
	@Override
	public LoginAssistant getLoginAssistant(Integer userId) {
		
		List<LoginAssistant> list = new ArrayList<LoginAssistant>();
		StringBuilder whereSql = new StringBuilder("SELECT u.LOGIN_NAME AS assistant_phone "
				+ "FROM visit_user u "
				+ "LEFT JOIN assitant a ON a.u_id = ? "
				+ "WHERE u.ID = a.a_id");
		
		List<Object> params = new ArrayList<Object>();
		params.add(userId);
		
		list = (List<LoginAssistant>) getListBySQL(whereSql.toString(),params.toArray(),
				LoginAssistant.class,null);
		
		if(!HelpFunctions.isEmpty(list)){
			return list.get(0);
		}
		return null;
	}

}
